using FastCode.Architecture;
using FastCode.Models;
using FastCode.DTO;
using FastCode.Helper;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FastCode.Controllers
{
    public class LogController : ApiControllerBase<Log>
    {
       readonly LogService logService;
       public LogController(ISqlSugarClient _db, LogService _logService) : base(_db)
       {
          logService = _logService;
       }

       [HttpPost]
       public override async Task<object> List([FromBody] QueryBase queryBase)
       {
           RefAsync<int> totalNumber = 0;
           var query = db.Queryable<Log>().Where(x => !x.IsDeleted);

           // 添加按 ID 倒序排序
           query = query.OrderByDescending(x => x.Id);

           // 解析 SearchCondition 并构建查询条件
           if (queryBase.SearchConditions != null && queryBase.SearchConditions.Any())
           {
               foreach (var searchConditions in queryBase.SearchConditions)
               {
                   foreach (var condition in searchConditions.ConditionalModels)
                   {
                       if (string.IsNullOrEmpty(condition.FieldValue))
                       {
                           continue;
                       }

                       if (condition.FieldName.EndsWith(ConstData.SplitStart))
                       {
                           condition.FieldName = condition.FieldName.Replace(ConstData.SplitStart, string.Empty);
                       }
                       else if (condition.FieldName.EndsWith(ConstData.SplitEnd))
                       {
                           condition.FieldName = condition.FieldName.Replace(ConstData.SplitEnd, string.Empty);
                       }
                       query = query.Where(new List<IConditionalModel>() { condition });
                   }
               }
           }

           var list = await query.ToPageListAsync(queryBase.pageIndex, queryBase.pageSize, totalNumber);
           var columnNames = new List<ColumnConfig>();

           return Msg.Success(new { list, columnNames = columnNames.OrderBy(x => x.Order), totalNumber = totalNumber.Value });
       }
    }
}
